package cn.chencaiju.day08;

import java.util.Set;
import java.util.TreeSet;

import org.junit.Test;

public class TreeSetTest {
	private Set<String> setStr=new TreeSet<>();
	private Set<Car> setCar=new TreeSet<>();
	@Test public void testOrder() {
		setStr.add("entry");
		setStr.add("order");
		setStr.add("traverse");
		for(String s:setStr) {
			System.out.println(s);
		}
		setCar.add(new Car("雪佛兰",4f,1.5f));		
		setCar.add(new Car("雪佛兰",4.1f,1.5f));
		setCar.add(new Car("雪佛兰",4.2f,1.5f));
		setCar.add(new Car("雪佛兰",4.3f,1.5f));
		for(Car c:setCar) {
			System.out.println(c);
		}
	}
	
	/*
	 * 求只出现一次的元素个数
	 */
	public int getNumOfSingle(String[] strs) {
		for(int i=0;i<strs.length;i++) {
			setStr.add(strs[i]);
		}
		return setStr.size();
	}
	@Test public void testNumOfSingle() {
		String[] strs= {"entry","split","traverse","anonymity","cancel",
						"entry","split"};
		for(String temp:strs) {
			System.out.print(temp+" ");
		}
		System.out.println("不重复出现的元素有："+getNumOfSingle(strs));
	}
	
	/*
	 * 是否有重复的元素
	 */
	public boolean hasDuplicated(int[] rows) {
		Set<Integer> setInt=new TreeSet<>();
		for(int i=0;i<rows.length;i++) {
			setInt.add(rows[i]);
		}
		return !(setInt.size()==rows.length);
	}
	@Test public void testHasDuplicated() {
		// int[] rows = {1, 2, 3, 4, 5, 6, 7, 8, 9};
		int[] rows = {1, 2, 3, 4, 5, 6, 4, 8, 9};
		System.out.println(hasDuplicated(rows));
	}
}
